package org.ainlolcat.ainscience.statistics.handlers.hitran;

import org.ainlolcat.ainscience.statistics.handlers.hitran.instfunc.InstrumentalFunction;
import org.ainlolcat.ainscience.statistics.handlers.hitran.instfunc.PsdVoigt1InstrumentalFunction;
import org.ainlolcat.ainscience.tools;
import org.apache.log4j.Logger;
import org.math.plot.Plot2DPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
* @author ainlolcat
*         Date: 5/1/13
*/
public class SpectrTStatisticRenderer extends JPanel {
    private static Logger log = Logger.getLogger(SpectrTStatisticRenderer.class);
    SpectrTStatistic handler;
    JTextField temperature;
    JTextField jtf_y0;
    JTextField jtf_A;
    JTextField jtf_width;
    JTextField jtf_mu;
    Plot2DPanel plot;

    public SpectrTStatisticRenderer(SpectrTStatistic handler){
        super();
        this.handler = handler;
        setLayout(new BorderLayout());
        initComponent();
    }

    private void initComponent(){
        JPanel controls = new JPanel();
        controls.setLayout(new FlowLayout());
        temperature = new JTextField(new Double(handler.T).toString());
        controls.add(temperature);

        jtf_width = new JTextField(new Double(handler.w).toString());
        controls.add(jtf_width);

        jtf_mu = new JTextField(new Double(handler.mu).toString());
        controls.add(jtf_mu);

        jtf_y0 = new JTextField(new Double(handler.y0).toString());
        controls.add(jtf_y0);

        jtf_A = new JTextField(new Double(handler.A).toString());
        controls.add(jtf_A);

        JButton draw = new JButton("draw");
        draw.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                drawCustom();
                getTopLevelAncestor().setVisible(true);
            }
        });
        controls.add(draw);
        JButton reset = new JButton("reset");
        reset.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                drawEvaluated();
                getTopLevelAncestor().setVisible(true);
            }
        });
        controls.add(reset);
        add(controls, BorderLayout.PAGE_START);
        JPanel stats = new JPanel();
        stats.setLayout(new FlowLayout());
        stats.add(new JTextField("Temp: " + handler.T));
        add(stats,BorderLayout.PAGE_END);
        drawEvaluated();
    }

    private void drawEvaluated(){
        double[][] data = handler.data;
        double[] x = new double[data[0].length];
        double[] y = new double[data[0].length];
        double[] I = new double[data[0].length];
        double[] eps = new double[data[0].length];
        double maxY = data[1][0];
        double maxS = data[2][0];
        for (int i=0;i<data[0].length;i++){
            if (data[1][i]>maxY) maxY = data[1][i];
            if (data[2][i]>maxS) maxS = data[2][i];
        }
        for (int i=0;i<data[0].length;i++){
            x[i] = data[0][i];
            y[i] = data[1][i]/maxY;
            I[i] = data[2][i]/maxS;
            eps[i] = y[i] - I[i];
        }
        if (plot!=null){
            remove(plot);
        }
        plot = new Plot2DPanel();
        plot.addLinePlot("", x, y);
        plot.addLinePlot("", x, I);
        plot.addLinePlot("", x, eps);
        add(plot, BorderLayout.CENTER);
    }

    private void drawCustom(){
        try{
            double[][] data = handler.data;
            double[] x = new double[data[0].length];
            double[] y = new double[data[0].length];
            double[] eps = new double[data[0].length];
            final double _y0 = tools.parseDouble(jtf_y0.getText());
            final double _A = tools.parseDouble(jtf_A.getText());
            final double _w = tools.parseDouble(jtf_width.getText());
            final double _mu = tools.parseDouble(jtf_mu.getText());
            double T = tools.parseDouble(temperature.getText());
            InstrumentalFunction func = new PsdVoigt1InstrumentalFunction(_y0,_A,_w,_mu);
            double[] I = handler.stf.getSpectr(T ,func,data[0],data[1]);
            double maxY = data[1][0];
            double maxS = I[0];
            for (int i=0;i<data[0].length;i++){
                if (data[1][i]>maxY) maxY = data[1][i];
                if (I[i]>maxS) maxS = I[i];
            }
            for (int i=0;i<data[0].length;i++){
                x[i] = data[0][i];
                y[i] = data[1][i]/maxY;
                I[i] = I[i]/maxS;
                eps[i] = y[i] - I[i];
            }
            if (plot!=null){
                remove(plot);
            }
            plot = new Plot2DPanel();
            plot.addLinePlot("", x, y);
            plot.addLinePlot("", x, I);
            plot.addLinePlot("", x, eps);
            add(plot, BorderLayout.CENTER);
        }catch(Exception e){
            log.error("Cannot draw custom ", e);
        }
    }
}
